 //Tao ra mot table Datagridview
package DAO;

import java.sql.*; 
import javax.swing.table.*; 
public class ResultsModel extends DefaultTableModel { 
   
    public void setResultSet(ResultSet results, String []title) { 
        try { 
           ResultSetMetaData metadata = results.getMetaData();
            int columns =  metadata.getColumnCount(); 
            // Get the column names and set header names 
            for (int i = 0; i < columns; i++) { 
              addColumn(title[i]); 
            } 
           // Get all rows 
            while (results.next()) { 
                String[] rowData = new String[columns];   // Create array to hold the data 
                for (int i = 0; i < columns; i++) {           // For each column 
                    rowData[ i ] = results.getString(i+1);     // retrieve the data item 
                } 
                addRow(rowData);          // Add a row 
            } 
            fireTableChanged(null);     // Signal the table there is new model data 
            
        } catch (SQLException sqle) { 
            System.err.println(sqle.getMessage()); 
        } 
    } 
}
